package com.csust.cg.mongodb.mapper;

import com.csust.cg.mongodb.domain.entity.Connect;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author chenguang
 */
public interface ConnectMapper {
    /**
     * 通过分组ID和用户ID获取保存的连接信息集合
     *
     * @param groupId 分组ID
     * @param userId  用户ID
     * @return 保存的连接集合
     */
    @Select("SELECT * FROM connect WHERE group_id = #{groupId} AND user_id = #{userId} AND type = 0")
    List<Connect> getConnectListByGroupIdAndUserId(@Param("groupId") Integer groupId, @Param("userId") String userId);

    /**
     * 通过用户最近的全部连接
     * @param userId 用户ID
     * @return 最近连接的集合
     */
    @Select("SELECT * FROM connect WHERE user_id = #{userId} AND type = 1 ORDER BY update_time DESC")
    List<Connect> selectAllRecentConnectListByUserIdOrderByCreateTime(String userId);

    /**
     * 通过ID获取连接信息
     * @param id 连接ID
     * @return 连接信息
     */
    @Select("SELECT * FROM connect WHERE id = #{id}")
    Connect selectConnectById(Integer id);

    /**
     * 插入连接信息
     *
     * @param connect 连接信息
     */
    @Insert("INSERT INTO " +
            "connect(user_id, group_id, type, name, protocol, ip, port, auth, username, password, authDatabase, authMechanism, update_time) " +
            "VALUES (#{userId}, #{groupId}, #{type}, #{name}, #{protocol}, #{ip}, #{port}, #{auth}, #{username}, #{password}, #{authDatabase}, #{authMechanism}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void insertSaveConnect(Connect connect);

    /**
     * 通过分组ID删除保存的连接信息
     *
     * @param groupId 分组ID
     * @return 影响行数
     */
    @Delete("DELETE FROM connect WHERE group_id = #{groupId}")
    Integer deleteByGroupId(@Param("groupId") Integer groupId);

    /**
     * 通过连接ID和用户ID更新连接信息
     *
     * @param connect 连接信息
     */
    @Update("UPDATE connect SET " +
            "group_id = #{groupId}, name = #{name}, protocol = #{protocol}, auth = #{auth}," +
            "ip = #{ip}, port = #{port}, username = #{username}, " +
            "password = #{password}, authDatabase = #{authDatabase}, " +
            "authMechanism = #{authMechanism}, update_time = #{updateTime} WHERE id = #{id} AND user_id = #{userId}")
    void updateSaveConnectByIdAndUserId(Connect connect);

    /**
     * 通过用户ID和连接ID集合删除连接
     *
     * @param userId 用户ID
     * @param ids    连接ID集合
     */
    @Delete("<script>"
            + "DELETE FROM connect WHERE user_id = #{userId} AND id IN "
            + "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>"
            + "#{item}"
            + "</foreach>"
            + "</script>")
    void deleteByUserIdAndIds(@Param("userId") String userId, @Param("ids") List<Integer> ids);

    /**
     * 通过用户ID删除所有最近连接
     * @param userId 用户ID
     */
    @Delete("DELETE FROM connect WHERE user_id = #{userId} AND type = 1")
    void deleteAllByUserIdAndType(String userId);

    /**
     * 通过用户ID和连接ID修改连接名和分组信息
     *
     * @param name    连接名
     * @param groupId 分组ID
     * @param userId  用户ID
     * @param id      连接ID
     */
    @Update("UPDATE connect SET name = #{name}, group_id = #{groupId} WHERE id = #{id} AND user_id = #{userId}")
    void updateSaveConnectNameAndGroupByUserIdAndId(@Param("name") String name, @Param("groupId") Integer groupId,
                                                    @Param("userId") String userId, @Param("id") Integer id);
}
